
using System;
using System.Collections.Generic;
using System.Text;
using MediaPortal.Dialogs;
using MediaPortal.GUI.Library;

namespace MyEmulators2
{
    class MenuPresenter
    {
        //Does all the logic behind the different context menus

        updatedPresenter guiHandler;

        public MenuPresenter(updatedPresenter guiHandler)
        {
            this.guiHandler = guiHandler;
        }

        public void showViewDialog(int windowID)
        {
            showViewDialog(windowID, false);
        }

        public void showViewDialog(int windowID, bool emulatorSelected)
        {
            GUIDialogMenu dlg = (GUIDialogMenu)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_DIALOG_MENU);
            if (dlg != null)
            {
                dlg.Reset();
                dlg.SetHeading(Translator.getString(TranslatorString.moreviews));
                int selectedId = 1;
                if (!emulatorSelected)
                {
                    dlg.Add(new GUIListItem(Translator.getString(TranslatorString.allgames)));
                    selectedId = 0;
                }
                dlg.Add(new GUIListItem(Translator.getString(TranslatorString.favourites)));
                dlg.Add(new GUIListItem(Translator.getString(TranslatorString.sortgenre)));
                dlg.Add(new GUIListItem(Translator.getString(TranslatorString.sortcompany)));
                dlg.Add(new GUIListItem(Translator.getString(TranslatorString.sortyeardesc)));
                dlg.Add(new GUIListItem(Translator.getString(TranslatorString.sortyearasc)));
                dlg.DoModal(windowID);
                selectedId += dlg.SelectedId;
                
                //Sort accordingly
                switch (selectedId)
                {
                    case 1:
                        guiHandler.FillSomeGames("ORDER BY title ASC", GameProperty.none, 0);
                        break;
                    case 2:
                        guiHandler.FillSomeGames("WHERE favourite='True' ORDER BY title ASC", GameProperty.none, 0, ViewState.Views, true); //special case for favourites so layout option is observed
                        break;
                    case 3:
                        guiHandler.FillSomeGames("ORDER BY genre ASC", GameProperty.genre, 0);
                        break;
                    case 4:
                        guiHandler.FillSomeGames("ORDER BY company ASC", GameProperty.company, 0);
                        break;
                    case 5:
                        guiHandler.FillSomeGames("ORDER BY yearmade DESC", GameProperty.year, 0);
                        break;
                    case 6:
                        guiHandler.FillSomeGames("ORDER BY yearmade ASC", GameProperty.year, 0);
                        break;
                }
            }
        }

        public void showGameDialog(int windowID, Game game)
        {
            GUIDialogMenu dlg = (GUIDialogMenu)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_DIALOG_MENU);
            if (dlg != null)
            {
                dlg.Reset();
                dlg.SetHeading(Options.Instance.GetStringOption("shownname"));

                dlg.Add(new GUIListItem(Translator.getString(TranslatorString.launchwithprofile)));
                if (game.Favourite)
                {
                    dlg.Add(new GUIListItem(Translator.getString(TranslatorString.favouriteunmark)));
                }
                else
                {
                    dlg.Add(new GUIListItem(Translator.getString(TranslatorString.favouritemark)));
                }
                dlg.Add(new GUIListItem(Translator.getString(TranslatorString.selectgrade)));
                dlg.DoModal(windowID);
                if (dlg.SelectedId == 1)
                {
                    showProfileDialog(windowID, game);
                }
                else if (dlg.SelectedId == 2)
                {
                    game.Favourite = !game.Favourite;
                    game.SaveGamePlayInfo();
                }
                else if (dlg.SelectedId == 3)
                {
                    if (showGradeDialog(windowID, game))
                    {
                        game.SaveGamePlayInfo();
                    }
                }

            }
        }

        private void showProfileDialog(int windowID, Game game)
        {
            GUIDialogMenu dlg = (GUIDialogMenu)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_DIALOG_MENU);
            if (dlg != null)
            {
                dlg.Reset();
                dlg.SetHeading("Emulator Profiles");

                EmulatorProfile[] profiles = DB.Instance.GetProfiles(game.ParentEmulator);
                for (int x = 0; x < profiles.Length; x++)
                {
                    dlg.Add(new GUIListItem(profiles[x].Title));
                }

                dlg.DoModal(windowID);

                int index = dlg.SelectedId - 1;
                if (index > -1 && index < profiles.Length)
                {
                    Executor.Instance.launchGame(game, profiles[index]);
                }
            }
        }

        private bool showGradeDialog(int windowID, Game game)
        {
            GUIDialogMenu dlg = (GUIDialogMenu)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_DIALOG_MENU);
            if (dlg != null)
            {
                dlg.Reset();
                dlg.SetHeading(Translator.getString(TranslatorString.selectgrade));
                for (int i = 1; i < 11; i++)
                {
                    dlg.Add(new GUIListItem(Translator.getString(TranslatorString.setgradeto) + " " + i));
                }
                dlg.Add(new GUIListItem(Translator.getString(TranslatorString.nograde)));
                dlg.DoModal(windowID);

                if (dlg.SelectedId >= 1 && dlg.SelectedId <= 10)
                {
                    game.Grade = dlg.SelectedId;
                    return true;
                }
                else if (dlg.SelectedId == 11)
                {
                    game.Grade = 0;
                    return true;
                }
            }
            return false;
        }

        public int showLayoutDialog(int windowID)
        {
            GUIDialogMenu dlg = (GUIDialogMenu)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_DIALOG_MENU);
            if (dlg != null)
            {
                dlg.Reset();
                dlg.SetHeading("Layouts Menu");
                dlg.Add(new GUIListItem("Layout: List"));
                dlg.Add(new GUIListItem("Layout: Icons"));
                dlg.Add(new GUIListItem("Layout: Big Icons"));
                dlg.Add(new GUIListItem("Layout: Filmstrip"));
                dlg.Add(new GUIListItem("Layout: Cover Flow"));
                dlg.DoModal(windowID);
                return dlg.SelectedLabel;
            }
            return -1;
        }


    }
}
